package cn.duli.xianer.controller;

import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.pojo.PrivateMsg;
import cn.duli.xianer.service.PrivateMsgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/privateMsg")
public class PrivateMsgController {

    @Autowired
    private PrivateMsgService privateMsgService;

    /**
     * 增加私信
     * @param privateMsg
     * @return
     */
    @PostMapping("/save")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult save(PrivateMsg privateMsg) {
        // 发送私信时，插入两份一样的数据（pmUser和pmFriend对调）
        privateMsgService.save(privateMsg);
        PrivateMsg privateMsg1 = new PrivateMsg();
        privateMsg1.setPmUser(privateMsg.getPmFriend());
        privateMsg1.setPmFriend(privateMsg.getPmUser());
        privateMsg1.setPmContent(privateMsg.getPmContent());
        privateMsg1.setPmReceiver(privateMsg.getPmReceiver());
        privateMsg1.setPmSender(privateMsg.getPmSender());
        privateMsg1.setPmTime(privateMsg.getPmTime());
        privateMsg1.setPmGid(privateMsg.getPmGid());
        privateMsg1.setPmWgid(privateMsg.getPmWgid());
        int pmId = privateMsgService.save(privateMsg1);
        PrivateMsg pMsg = privateMsgService.findById(pmId);
        return new MyResult(200, "增加私信",null,null,pMsg,null);
    }

    /**
     * 根据用户的发送者id(pmUser)查看该用户与其他用户的私信列表
     * @param pmUser
     * @return
     */
    @GetMapping("/findAllByUser")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findAllByUser(@RequestParam("pmUser") Integer pmUser) {
        List<PrivateMsg> list = privateMsgService.findAllByUser(pmUser);
        List<PrivateMsg> newList = new ArrayList<>();
        for (int i=0;i<list.size();i++) {
            PrivateMsg privateMsg = privateMsgService.findById(list.get(i).getPmId());
            // 查看两用户间未读消息数量
            int count = privateMsgService.findNotReadCount(pmUser, privateMsg.getPmFriend());
            privateMsg.setMsgCount(count);
            newList.add(privateMsg);
        }
        return new MyResult(200, "根据用户的发送者id(pmUser)查看该用户与其他用户的私信列表",null,newList,null,null);
    }

    /**
     * 根据私信id查看私信详情
     * @param pmId
     * @return
     */
    @GetMapping("/findById/{pmId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findById(@PathVariable("pmId") Integer pmId) {
        PrivateMsg privateMsg = privateMsgService.findById(pmId);
        return new MyResult(200, "根据私信id查看私信详情",null,null,privateMsg,null);
    }

    /**
     * 根据用户的发送者id(pmUser)和接收者id(pmFriend)查看两用户私信详情
     * @param pmUser
     * @param pmFriend
     * @return
     */
    @GetMapping("/findByUserAndFriend")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findByUserAndFriend(@RequestParam("pmUser") Integer pmUser,
                                        @RequestParam("pmFriend") Integer pmFriend,
                                        @RequestParam(defaultValue = "1") int pageNo,
                                        @RequestParam(defaultValue = "6") int pageSize) {
        // 先查询未读消息列表
        List<PrivateMsg> list = privateMsgService.findNotRead(pmUser, pmFriend);
        for (int i=0;i<list.size();i++) {
            PrivateMsg privateMsg = list.get(i);
            // 修改阅读时间和阅读状态
            privateMsgService.updateRead(privateMsg.getPmId());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<PrivateMsg> pageInfo = new PageInfo<>(privateMsgService.findByUserAndFriend(pmUser, pmFriend));
        return new MyResult(200, "根据用户的发送者id(pmUser)和接收者id(pmFriend)查看两用户私信详情",null,null,null,pageInfo);
    }

    /**
     * 根据私信id删除此条私信
     * @param pmId
     * @return
     */
    @RequestMapping("/deleteById/{pmId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult deleteById(@PathVariable("pmId") Integer pmId) {
        privateMsgService.deleteById(pmId);
        return new MyResult(200, "根据私信id删除此条私信",null,null,null,null);
    }

    /**
     * 私信列表页面删除两指定用户间的整个会话
     * @param pmUser
     * @param pmFriend
     * @return
     */
    @RequestMapping("/deleteAll")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult deleteAll(@RequestParam("pmUser") Integer pmUser,
                              @RequestParam("pmFriend") Integer pmFriend) {
        privateMsgService.deleteAll(pmUser, pmFriend);
        return new MyResult(200, "私信列表页面删除两指定用户间的整个会话",null,null,null,null);
    }
}
